//
//  JanpanViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/8.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "JanpanViewController.h"

@interface JanpanViewController ()

@end

@implementation JanpanViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    _ArrOfmusicID = [[NSMutableArray alloc] init];
    _DetailArr = [[NSMutableArray alloc] init];
    _normalView = [[NormalView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _normalView.tableView.dataSource = self;
    _normalView.tableView.delegate = self;
    _normalView.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_normalView];
    [_normalView release];
    
    
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.removeFromSuperViewOnHide = YES;
    HUD.labelText = @"loading...";
    [self.view addSubview:HUD];
    HUD.mode = MBProgressHUDModeIndeterminate;
    
    [self getMusicData];
    
    Legacy = [SinglePlayer shareHandleSinglePlayer];
    BarPlayer = [DanLiPlayer shareHandleDanLiPlayer:CGRectZero];
    [self.view addSubview:BarPlayer];
    




}

// 下拉效果
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 改变垂直偏移量
    CGFloat yy = scrollView.contentOffset.y;
    if (yy <= 0) {
        // 取正值
        yy = 0 - yy;
        _normalView.effectView.frame = CGRectMake(0 - yy , 0 - yy, self.view.frame.size.width + yy + yy, 150 + yy);
    } 
}

- (void)getMusicData
{
    
    [AFN urlString:Detailpic_url getAFNdata:^(id responseObject) {

        NSArray *dataArr = [responseObject objectForKey:@"data"];
        
        for (NSDictionary *dic in dataArr) {
            
            if ([[dic objectForKey:@"title"] isEqualToString:@"日韩"]) {
                
                NSString *one = [dic objectForKey:@"msg_id"];
                NSString *two = @"http://v1.ard.q.itlily.com/share/user_timeline?msg_ids=%5B";
                JanpanUrl = [NSString stringWithFormat:@"%@%@", two, one];
                [_normalView getURLForImage:[dic objectForKey:@"pic"] textForLabel:nil];
            }
        }
        
        [AFN urlString:JanpanUrl getAFNdata:^(id responseObject) {
            
            NSArray *dataArr = [responseObject objectForKey:@"data"];
            for (NSDictionary *dic in dataArr) {
                NSArray *songListArr = [dic objectForKey:@"songlist"];
                for (NSDictionary *littleDic in songListArr) {
                    NSString *song_id = [littleDic objectForKey:@"song_id"];
                    [_ArrOfmusicID addObject:song_id];
                }
            }
            NSMutableString *MuIdString = [NSMutableString stringWithString:@""];
            for (int i = 0; i< _ArrOfmusicID.count; i++) {
                NSString *one = [_ArrOfmusicID objectAtIndex:i];
                NSString *two = [NSString stringWithFormat:@"%@," ,one];
                [MuIdString appendString:two];
            }
            // 拼接字符串 的网址
            url_string = [NSString stringWithFormat:@"http://ting.hotchanson.com/songs/downwhite?song_id=%@", MuIdString];
            [AFN urlString:url_string getAFNdata:^(id responseObject) {
                
                NSArray *dataArr = [responseObject objectForKey:@"data"];
                for (NSDictionary *dic in dataArr) {
                    ReDetailModel *reDetail = [[ReDetailModel alloc] init];
                    [reDetail setValuesForKeysWithDictionary:dic];
                    [_DetailArr addObject:reDetail];
                    [reDetail release];
                }
                HUD.hidden = YES;
                _normalView.tableView.separatorColor = [UIColor lightGrayColor];
                [_normalView.tableView reloadData];
                
            }];
        }];
    }];
    
    
        
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _DetailArr.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    ReDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == NO) {
        
        cell = [[ReDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    if (_DetailArr.count > indexPath.row) {
        ReDetailModel *reDic = [_DetailArr objectAtIndex:indexPath.row];
        
        [cell getDataForCell:reDic];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%ld", (long)indexPath.row+1];

    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return SCREEN_WIDTH*0.213333;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ReDetailModel *reDetailmodel = [_DetailArr objectAtIndex:indexPath.row];
    BarPlayer.reDetailModel = reDetailmodel;
    BarPlayer.dataArr = _DetailArr;
    BarPlayer.index = indexPath.row;
    [self playeMusicWithModel:reDetailmodel index:indexPath.row];
}


// 播放方法
- (void)playeMusicWithModel:(ReDetailModel *)model index:(NSInteger)index
{
    if (model.audition_list.count > 1) {
        NSDictionary *dic = [model.audition_list objectAtIndex:1];
        NSString *url = [dic objectForKey:@"url"];
        [Legacy play:url];
        
    } else {
        NSDictionary *dic = [model.audition_list objectAtIndex:0];
        NSString *url= [dic objectForKey:@"url"];
        [Legacy play:url];
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
